import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:liuxlcommon/lget/LGetScaffold.dart';
import 'package:liuxlcommon/lget/LState.dart';
import 'package:liuxlcommon/util/sp_utils.dart';
import 'package:love_newf/app/com/view/my_app_bar.dart';
import 'package:love_newf/app/com/view/my_app_bar2.dart';
import 'package:love_newf/app/page/chat/chat_logic.dart';
import 'package:love_newf/app/page/live/live_logic.dart';
import 'package:love_newf/app/page/login/login_logic.dart';
import 'package:love_newf/app/page/message_list/conversation_list/conversation_list_logic.dart';
import 'package:love_newf/app/page/mine/setting/setting_logic.dart';
import 'package:love_newf/app/page/open_live_setting/open_live_setting_logic.dart';
import 'package:love_newf/app/page/search/search_logic.dart';
import 'package:love_newf/app/page/search/search_view.dart';

bool lazyInit = false;

lazyLogic() {
  if (lazyInit) return;
  lazyInit = true;
  Get.lazyPut(() => ConversationListLogic());
}

createLogic(Type? type) {
  lazyLogic();
  var s = type.toString();
  if (s == "LoginLogic") {
    return Get.put(LoginLogic());
  } else if (s == "OpenLiveSettingLogic") {
    return Get.put(OpenLiveSettingLogic());
  } else if (s == "SearchLogic") {
    return Get.put(SearchLogic());
  } else if (s == "LiveLogic") {
    return Get.put(LiveLogic());
  } else if (s == "SettingLogic") {
    return Get.put(SettingLogic());
  } else if (s == "ChatLogic") {
    return Get.put(ChatLogic());
  }
  return null;
}

abstract class BaseState<T extends StatefulWidget, L extends GetxController>
    extends LState<T> {
  late L logic;
  String title = ""; //标题
  var hideAppBar = false; //是否隐藏导航栏
  ///全屏
  var extendBodyBehindAppBar = false;
  var resizeToAvoidBottomInset = false;

  var _isFirst = true;

  get initData;

  Widget get easyBody;

  @override
  Widget build(BuildContext context) {
    try {
      if (logic == null) {}
    } catch (e) {
      var l = createLogic(L);
      if (l != null) {
        logic = l;
      }
    }
    if (_isFirst) {
      _isFirst = false;
      initData;
    }
    return super.build(context);
  }

  @override
  get init {}

  @override
  get body {
    return LGetScaffold<L>(
        extendBodyBehindAppBar: extendBodyBehindAppBar,
        resizeToAvoidBottomInset: resizeToAvoidBottomInset,
        appBar: hideAppBar
            ? null
            : extendBodyBehindAppBar
                ? MyAppBarTransparent(title)
                : MyAppBar(title),
        builder: () => easyBody);
  }

  @override
  get finish {
    Get.delete<L>();
  }
}
